/*
 * Copyright (C) 2016 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See http://www.gnu.org/licenses/gpl-2.0.html for more details.
 */

#ifdef BUILD_LK
#include <platform/mt_gpio.h>
#include <platform/mt_i2c.h>
#include <platform/mt_pmic.h>
#include <string.h>
#else
#include <linux/string.h>
#include <linux/wait.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/pinctrl/consumer.h>
#include <linux/of_gpio.h>
#include <asm-generic/gpio.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/mm_types.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/uaccess.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/platform_device.h>

#ifdef CONFIG_OF
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/regulator/consumer.h>
#endif
#endif
#include "lcm_drv.h"
#include "rt4801_i2c.h"

#ifndef BUILD_LK
static unsigned int GPIO_LCD_RST;
static unsigned int GPIO_CTP_RST;
static unsigned int GPIO_LCD_PWR_ENP;
static unsigned int GPIO_LCD_PWR_ENN;
static struct regulator *lcm_vgp;

void lcm_request_gpio_control(struct device *dev)
{
	pr_notice("[KE/LCM] %s() enter\n", __func__);

	GPIO_LCD_RST = of_get_named_gpio(dev->of_node, "gpio_lcd_rst", 0);
	gpio_request(GPIO_LCD_RST, "GPIO_LCD_RST");
	GPIO_CTP_RST = of_get_named_gpio(dev->of_node, "gpio_ctp_rst", 0);
	gpio_request(GPIO_CTP_RST, "GPIO_CTP_RST");
	GPIO_LCD_PWR_ENP = of_get_named_gpio(dev->of_node,
		"gpio_lcd_pwr_enp", 0);
	gpio_request(GPIO_LCD_PWR_ENP, "GPIO_LCD_PWR_ENP");
	GPIO_LCD_PWR_ENN = of_get_named_gpio(dev->of_node,
		"gpio_lcd_pwr_enn", 0);
	gpio_request(GPIO_LCD_PWR_ENN, "GPIO_LCD_PWR_ENN");
}

/* get VTP LDO supply */
static int lcm_get_vgp_supply(struct device *dev)
{
	int ret;
	struct regulator *lcm_vgp_ldo;

	pr_notice("[KE/LCM] %s() enter\n", __func__);

	lcm_vgp_ldo = devm_regulator_get(dev, "reg-lcm");
	if (IS_ERR(lcm_vgp_ldo)) {
		ret = PTR_ERR(lcm_vgp_ldo);
		pr_notice("failed to get reg-lcm LDO, %d\n", ret);
		return ret;
	}

	pr_notice("LCM: lcm get supply ok.\n");

	ret = regulator_enable(lcm_vgp_ldo);
	/* get current voltage settings */
	ret = regulator_get_voltage(lcm_vgp_ldo);
	pr_notice("lcm LDO voltage = %d in LK stage\n", ret);

	lcm_vgp = lcm_vgp_ldo;

	return ret;
}

int lcm_vgp_supply_enable(void)
{
	int ret;
	unsigned int volt;

	pr_notice("[KE/LCM] %s() enter\n", __func__);

	if (lcm_vgp == NULL)
		return 0;

	pr_notice("LCM: set regulator voltage lcm_vgp voltage to 1.8V\n");
	/* set voltage to 1.8V */
	ret = regulator_set_voltage(lcm_vgp, 1800000, 1800000);
	if (ret != 0) {
		pr_notice("LCM: lcm failed to set lcm_vgp voltage: %d\n", ret);
		return ret;
	}

	/* get voltage settings again */
	volt = regulator_get_voltage(lcm_vgp);
	if (volt == 1800000)
		pr_notice("LCM: check regulator voltage=1800000 pass!\n");
	else
		pr_notice("LCM: check regulator voltage=1800000 fail! (voltage: %d)\n",
			volt);

	ret = regulator_enable(lcm_vgp);
	if (ret != 0) {
		pr_notice("LCM: Failed to enable lcm_vgp: %d\n", ret);
		return ret;
	}

	return ret;
}

int lcm_vgp_supply_disable(void)
{
	int ret = 0;
	unsigned int isenable;

	if (lcm_vgp == NULL)
		return 0;

	/* disable regulator */
	isenable = regulator_is_enabled(lcm_vgp);

	pr_notice("LCM: lcm query regulator enable status[%d]\n",
		isenable);

	if (isenable) {
		ret = regulator_disable(lcm_vgp);
		if (ret != 0) {
			pr_notice("LCM: lcm failed to disable lcm_vgp: %d\n",
				ret);
			return ret;
		}
		/* verify */
		isenable = regulator_is_enabled(lcm_vgp);
		if (!isenable)
			pr_notice("LCM: lcm regulator disable pass\n");
	}

	return ret;
}

static int lcm_driver_probe(struct device *dev, void const *data)
{
	pr_notice("[KE/LCM] %s() enter\n", __func__);

	lcm_get_vgp_supply(dev);
	lcm_request_gpio_control(dev);
	return 0;
}

static const struct of_device_id lcm_platform_of_match[] = {
	{
		.compatible = "ft,ft8201",
		.data = 0,
	}, {
		/* sentinel */
	}
};

MODULE_DEVICE_TABLE(of, platform_of_match);

static int lcm_platform_probe(struct platform_device *pdev)
{
	const struct of_device_id *id;

	id = of_match_node(lcm_platform_of_match, pdev->dev.of_node);
	if (!id)
		return -ENODEV;

	return lcm_driver_probe(&pdev->dev, id->data);
}

static struct platform_driver lcm_driver = {
	.probe = lcm_platform_probe,
	.driver = {
		.name = "ft8201",
		.owner = THIS_MODULE,
		.of_match_table = lcm_platform_of_match,
	},
};

static int __init lcm_drv_init(void)
{
	pr_notice("LCM: Register lcm driver\n");
	if (platform_driver_register(&lcm_driver)) {
		pr_notice("LCM: failed to register disp driver\n");
		return -ENODEV;
	}

	return 0;
}

static void __exit lcm_drv_exit(void)
{
	platform_driver_unregister(&lcm_driver);
	pr_notice("LCM: Unregister lcm driver done\n");
}

late_initcall(lcm_drv_init);
module_exit(lcm_drv_exit);
MODULE_AUTHOR("mediatek");
MODULE_DESCRIPTION("Display subsystem Driver");
MODULE_LICENSE("GPL");
#endif


/* ----------------------------------------------------------------- */
/*  Local Constants */
/* ----------------------------------------------------------------- */
#define LCM_DSI_CMD_MODE           0
#define FRAME_WIDTH                (1200)
#define FRAME_HEIGHT               (1920)
#define GPIO_OUT_ONE               1
#define GPIO_OUT_ZERO              0
#define REGFLAG_DELAY              0xFE
#define REGFLAG_END_OF_TABLE       0xFF

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

/* ----------------------------------------------------------------- */
/*  Local Variables */
/* ----------------------------------------------------------------- */
static struct LCM_UTIL_FUNCS lcm_util = {0};
#define SET_RESET_PIN(v)     (lcm_util.set_reset_pin((v)))
#define UDELAY(n)            (lcm_util.udelay(n))
#define MDELAY(n)            (lcm_util.mdelay(n))

/* ----------------------------------------------------------------- */
/*  Local Functions */
/* ----------------------------------------------------------------- */
#define dsi_set_cmdq_V2(cmd, count, ppara, force_update) \
	lcm_util.dsi_set_cmdq_V2(cmd, count, ppara, force_update)
#define dsi_set_cmdq(pdata, queue_size, force_update) \
	lcm_util.dsi_set_cmdq(pdata, queue_size, force_update)
#define wrtie_cmd(cmd)      lcm_util.dsi_write_cmd(cmd)
#define write_regs(addr, pdata, byte_nums) \
	lcm_util.dsi_write_regs(addr, pdata, byte_nums)
#define read_reg(cmd)       lcm_util.dsi_dcs_read_lcm_reg(cmd)
#define read_reg_v2(cmd, buffer, buffer_size) \
	lcm_util.dsi_dcs_read_lcm_reg_v2(cmd, buffer, buffer_size)

#ifdef BUILD_LK
#define LCD_DEBUG(fmt)  dprintf(CRITICAL, fmt)
#else
#define LCD_DEBUG(fmt)  pr_debug(fmt)
#endif

static void lcm_set_gpio_output(unsigned int GPIO, unsigned int output)
{
#ifdef BUILD_LK
	mt_set_gpio_mode(GPIO, GPIO_MODE_00);
	mt_set_gpio_dir(GPIO, GPIO_DIR_OUT);
	mt_set_gpio_out(GPIO, output);
#else
	gpio_direction_output(GPIO, output);
	gpio_set_value(GPIO, output);
#endif
}

struct LCM_setting_table {
	unsigned char cmd;
	unsigned char count;
	unsigned char para_list[64];
};

/* Panel initial code is default in FT8201 flash.
 * So only need control power sequence if we needn't update initial.
 * No need update initial code: FTS_AUTO_LIC_UPGRADE_EN=0(default)
 * Need update initial code: FTS_AUTO_LIC_UPGRADE_EN=1(enable)
 * As backlight is from panel control, so initial code is needed.
 */

static struct LCM_setting_table lcm_init_setting[] = {
	//Stop reload
	{0x50, 1, {0x5A} },
	//INT CANCEL
	{0x54, 1, {0x03} },
	//VCOM
	{0x50, 2, {0x5A, 0x03} },
	{0x80, 1, {0xC2} },
	//Blank 8
	{0x50, 2, {0x5A, 0x08} },
	{0x80, 2, {0xC8, 0x00} },
	//Blank 9
	{0x50, 2, {0x5A, 0x09} },
	{0x80, 16, {0x5A, 0x51, 0xB5, 0xAA, 0x6A, 0xD5, 0x4A, 0x01,
		0x00, 0xE1, 0x0D, 0x82, 0x20, 0x08, 0x00, 0xB4} },
	{0x90, 16, {0x00, 0x24, 0x42, 0x0A, 0xE3, 0x6D, 0x1B, 0xF1,
		0xB9, 0xDC, 0x6B, 0x20, 0x69, 0xA1, 0x26, 0x00} },
	{0xA0, 6, {0xA1, 0x80, 0x08, 0x20, 0x06, 0x00} },
	//Blank 10
	{0x50, 2, {0x5A, 0x0A} },
	{0x80, 16, {0x62, 0xC5, 0x02, 0x09, 0x17, 0x23, 0x32, 0x43,
		0x55, 0x5B, 0x8E, 0x6C, 0xA1, 0x9C, 0x6E, 0x95} },
	{0x90, 16, {0x66, 0x60, 0x4E, 0x3C, 0x29, 0x17, 0x08, 0x09,
		0x17, 0x23, 0x32, 0x43, 0x55, 0x5B, 0x8E, 0x6C} },
	{0xA0, 12, {0xA1, 0x9C, 0x6E, 0x95, 0x66, 0x60, 0x4E, 0x3C,
		0x29, 0x17, 0x08, 0x00} },
	//Blank 11
	{0x50, 2, {0x5A, 0x0B} },
	{0x80, 16, {0x99, 0x99, 0x29, 0x44, 0x08, 0x00, 0x60, 0x47,
		0x00, 0x00, 0x10, 0x22, 0x04, 0x00, 0xB0, 0x03} },
	{0x90, 2, {0x15, 0x00} },
	//Blank 12
	{0x50, 2, {0x5A, 0x0C} },
	{0x80, 16, {0xFD, 0x68, 0x58, 0x02, 0x3C, 0x64, 0xB8, 0x06,
		0x00, 0x60, 0x15, 0x00, 0x50, 0x15, 0x56, 0x51} },
	{0x90, 16, {0x15, 0x55, 0x61, 0x15, 0x00, 0x60, 0x15, 0x00,
		0x50, 0x15, 0x56, 0x51, 0x15, 0x55, 0x61, 0x95} },
	{0xA0, 16, {0xAB, 0x18, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
		0x80, 0x4C, 0x29, 0x84, 0x52, 0x01, 0x09, 0x00} },
	//Blank 13
	{0x50, 2, {0x5A, 0x0D} },
	{0x80, 14, {0xF0, 0xB1, 0x71, 0xEF, 0x49, 0xC0, 0x80, 0x1A,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	//Blank 14
	{0x50, 2, {0x5A, 0x0E} },
	{0x80, 12, {0xFF, 0x81, 0x68, 0xAC, 0x22, 0x6D, 0x24, 0x00,
		0x00, 0x00, 0x00, 0x00} },
	//Blank 15
	{0x50, 2, {0x5A, 0x0F} },
	{0x80, 16, {0x0D, 0x08, 0x4A, 0x00, 0x25, 0x80, 0x64, 0x0C,
		0x51, 0x21, 0xC8, 0x50, 0x03, 0x4A, 0x8C, 0x8C} },
	{0x90, 7, {0x24, 0x69, 0x81, 0x71, 0x44, 0x7C, 0x14} },
	//Blank 16
	{0x50, 2, {0x5A, 0x10} },
	{0x80, 16, {0x00, 0x00, 0x03, 0xE7, 0x1F, 0x17, 0x00, 0x78,
		0x00, 0x03, 0xD4, 0x18, 0x30, 0x88, 0x41, 0x8A} },
	{0x90, 15, {0x39, 0x28, 0xA9, 0xC5, 0x9A, 0x7B, 0xF5, 0x07,
		0x7E, 0xE0, 0x07, 0x7E, 0x20, 0x10, 0x00} },
	//Blank 17
	{0x50, 2, {0x5A, 0x11} },
	{0x80, 16, {0x43, 0x77, 0x03, 0x40, 0x8A, 0xF1, 0xFF, 0x43,
		0x30, 0x08, 0xC1, 0x46, 0xA0, 0x80, 0x24, 0x18} },
	{0x90, 16, {0x30, 0x06, 0x64, 0xC1, 0x80, 0x21, 0x07, 0xC4,
		0xE1, 0xD0, 0x68, 0x40, 0x20, 0x1D, 0x04, 0x48} },
	{0xA0, 16, {0x40, 0x40, 0x28, 0x10, 0x90, 0x70, 0x38, 0x1A,
		0x10, 0x47, 0x03, 0xE2, 0x20, 0x00, 0x1C, 0x80} },
	{0xB0, 16, {0x50, 0x30, 0x00, 0xE0, 0xE1, 0x01, 0x00, 0x28,
		0x0E, 0x06, 0xC3, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC} },
	{0xC0, 16, {0x8C, 0x88, 0x88, 0x88, 0x88, 0x88, 0xC8, 0x08,
		0x86, 0xC6, 0xE3, 0x81, 0x00, 0x20, 0x00, 0x21} },
	{0xD0, 16, {0x42, 0x88, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
		0x00, 0x31, 0x04, 0x41, 0x06, 0x00, 0x00, 0x00} },
	{0xE0, 16, {0x00, 0x92, 0x24, 0x49, 0x92, 0x04, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x85, 0x11, 0x0C} },
	{0xF0, 9, {0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x5E,
		0x00} },
	//Blank 18
	{0x50, 2, {0x5A, 0x12} },
	{0x80, 16, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0x90, 9, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00} },
	//Blank 19
	{0x50, 2, {0x5A, 0x13} },
	{0x80, 16, {0x01, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00,
		0x01, 0x02, 0x01, 0x01, 0x44, 0x0C, 0xCE, 0xE7} },
	{0x90, 3, {0x62, 0x06, 0x00} },
	//Blank 20
	{0x50, 2, {0x5A, 0x14} },
	{0x80, 16, {0x51, 0x01, 0x40, 0x5E, 0xE9, 0xEF, 0xF7, 0xFB,
		0xFD, 0x7E, 0x00, 0x00, 0x00, 0xE0, 0xC5, 0x86} },
	{0x90, 16, {0x1B, 0x2C, 0x20, 0x2E, 0xDC, 0x60, 0x01, 0x31,
		0xE1, 0x06, 0x0B, 0x88, 0x07, 0x37, 0x58, 0x80} },
	{0xA0, 16, {0x39, 0xB0, 0xC1, 0x08, 0xCC, 0x80, 0x0D, 0x46,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0xB0, 16, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x24, 0x3C, 0xC2, 0x2B, 0x00, 0x40, 0xA1} },
	{0xC0, 16, {0x50, 0x78, 0x03, 0x6E, 0xB0, 0x80, 0x31, 0x00,
		0x2F, 0xC0, 0x0D, 0x16, 0x30, 0x06, 0xE0, 0x15} },
	{0xD0, 16, {0xB8, 0xC1, 0x02, 0xC6, 0x00, 0xBC, 0x04, 0x37,
		0x58, 0xC0, 0x18, 0x80, 0x37, 0xE1, 0x06, 0x0B} },
	{0xE0, 16, {0x18, 0x03, 0xF0, 0x1E, 0xDC, 0x60, 0x01, 0x63,
		0x00, 0xDE, 0x82, 0x1B, 0x2C, 0x60, 0x0C, 0xC0} },
	{0xF0, 16, {0x3B, 0x70, 0x83, 0x05, 0x8C, 0x01, 0x00, 0x01,
		0xBF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	//Blank 21
	{0x50, 2, {0x5A, 0x15} },
	{0x80, 16, {0x00, 0x68, 0x62, 0xE0, 0x06, 0x03, 0x03, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0x90, 16, {0x00, 0x00, 0xC7, 0x6B, 0x83, 0x38, 0xBE, 0x86,
		0xD0, 0x6A, 0x1F, 0x31, 0x6A, 0xB5, 0x21, 0xB4} },
	{0xA0, 16, {0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0xB0, 16, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x13, 0x00, 0xF0, 0x00, 0x14, 0x82, 0x00, 0x00} },
	{0xC0, 16, {0x08, 0xA2, 0xA8, 0x75, 0x5D, 0x57, 0x04, 0x61,
		0x18, 0x55, 0x55, 0xD5, 0x75, 0x55, 0x14, 0xC7} },
	{0xD0, 16, {0xB1, 0x2C, 0x49, 0x52, 0xD5, 0x75, 0x35, 0x0C,
		0x41, 0x50, 0xD5, 0x75, 0x5D, 0x57, 0xC3, 0x10} },
	{0xE0, 16, {0x04, 0x49, 0xB2, 0xAC, 0x75, 0x5D, 0x57, 0x45,
		0x71, 0x1C, 0x55, 0x55, 0xD5, 0x75, 0x45, 0x10} },
	{0xF0, 16, {0x86, 0xA1, 0x28, 0x08, 0x52, 0xD5, 0x75, 0x25,
		0x08, 0x00, 0x50, 0xD5, 0x75, 0x5D, 0x57, 0x02} },
	//Blank 22
	{0x50, 2, {0x5A, 0x16} },
	{0x80, 16, {0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0x90, 6, {0x00, 0x00, 0x00, 0x00, 0xC0, 0x40} },
	//Blank 24
	{0x50, 2, {0x5A, 0x18} },
	{0x80, 16, {0xEF, 0xBD, 0xF7, 0xDE, 0x7B, 0xEF, 0xBD, 0x07,
		0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08} },
	{0x90, 16, {0xA8, 0x09, 0xA8, 0x99, 0x08, 0x08, 0x08, 0x08,
		0x08, 0x08, 0x08, 0x08, 0x18, 0x09, 0x18, 0x99} },
	{0xA0, 16, {0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
		0x98, 0x08, 0x98, 0x08, 0x04, 0xFF, 0x05, 0x80} },
	{0xB0, 15, {0x80, 0x00, 0x04, 0x20, 0x00, 0x01, 0x08, 0x40,
		0x00, 0x02, 0x10, 0x80, 0x00, 0x04, 0x0F} },
	//Blank 25
	{0x50, 2, {0x5A, 0x19} },
	{0x80, 16, {0x95, 0x93, 0x8F, 0x8C, 0x80, 0x8B, 0x89, 0x87,
		0x86, 0x84, 0x83, 0x83, 0x00, 0x60, 0xF6, 0xFF} },
	{0x90, 16, {0xFF, 0x2F, 0xF3, 0xEF, 0xFF, 0xBF, 0x0F, 0x0F,
		0xAF, 0xB5, 0x71, 0x0E, 0x6C, 0x4A, 0x69, 0x08} },
	{0xA0, 7, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	//Blank 26
	{0x50, 2, {0x5A, 0x1A} },
	{0x80, 16, {0x00, 0x04, 0x08, 0x0C, 0x00, 0x10, 0x14, 0x18,
		0x1C, 0x00, 0x20, 0x28, 0x30, 0x38, 0x00, 0x40} },
	{0x90, 16, {0x48, 0x50, 0x58, 0x00, 0x60, 0x68, 0x70, 0x78,
		0x00, 0x80, 0x88, 0x90, 0x98, 0x00, 0xA0, 0xA8} },
	{0xA0, 16, {0xB0, 0xB8, 0x00, 0xC0, 0xC8, 0xD0, 0xD8, 0x00,
		0xE0, 0xE8, 0xF0, 0xF8, 0x00, 0xFC, 0xFE, 0xFF} },
	{0xB0, 16, {0x00, 0x00, 0x04, 0x08, 0x0C, 0x00, 0x10, 0x14,
		0x18, 0x1C, 0x00, 0x20, 0x28, 0x30, 0x38, 0x00} },
	{0xC0, 16, {0x40, 0x48, 0x50, 0x58, 0x00, 0x60, 0x68, 0x70,
		0x78, 0x00, 0x80, 0x88, 0x90, 0x98, 0x00, 0xA0} },
	{0xD0, 16, {0xA8, 0xB0, 0xB8, 0x00, 0xC0, 0xC8, 0xD0, 0xD8,
		0x00, 0xE0, 0xE8, 0xF0, 0xF8, 0x00, 0xFC, 0xFE} },
	{0xE0, 16, {0xFF, 0x00, 0x00, 0x04, 0x08, 0x0C, 0x00, 0x10,
		0x14, 0x18, 0x1C, 0x00, 0x20, 0x28, 0x30, 0x38} },
	{0xF0, 16, {0x00, 0x40, 0x48, 0x50, 0x58, 0x00, 0x60, 0x68,
		0x70, 0x78, 0x00, 0x80, 0x88, 0x90, 0x98, 0x00} },
	//Blank 27
	{0x50, 2, {0x5A, 0x1B} },
	{0x80, 16, {0xA0, 0xA8, 0xB0, 0xB8, 0x00, 0xC0, 0xC8, 0xD0,
		0xD8, 0x00, 0xE0, 0xE8, 0xF0, 0xF8, 0x00, 0xFC} },
	{0x90, 4, {0xFE, 0xFF, 0x00, 0x00} },
	//Blank 32
	{0x50, 2, {0x5A, 0x20} },
	{0x80, 7, {0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	//Blank 34
	{0x50, 2, {0x5A, 0x22} },
	{0x80, 13, {0x2D, 0xD3, 0x00, 0x41, 0x10, 0x04, 0x41, 0x10,
		0x04, 0x41, 0x10, 0x1E, 0x00} },
	//Blank 35
	{0x50, 2, {0x5A, 0x23} },
	{0x80, 16, {0x01, 0x05, 0x00, 0x05, 0x00, 0x00, 0x01, 0x00,
		0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0x90, 9, {0xFF, 0x0F, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00,
		0x00} },
	//Blank 36
	{0x50, 2, {0x5A, 0x24} },
	{0x80, 16, {0x00, 0x03, 0x00, 0x9D, 0xBF, 0x10, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF9, 0x7B} },
	//CABC control
	//{0x50, 2, {0x5A, 0x23} },
	//{0x95, 1, {0x01} },
	//SPI not FINISH
	//{0x90, 1, {0x5A} },
	//SPI FINISH
	{0x90, 1, {0xA5} },
	//Blank select 2F
	{0x50, 2, {0x5A, 0x2F} },
	{0x09, 1, {0x1} },
	{0x11, 0, { } },
	{REGFLAG_DELAY, 120, {} },
	{0x29, 0, { } },
};

static struct LCM_setting_table lcm_suspend_setting[] = {
	{0x28, 0, {} },
	{REGFLAG_DELAY, 20, {} },
	{0x10, 0, {} },
	{REGFLAG_DELAY, 160, {} },
};

static void push_table(struct LCM_setting_table *table,
	unsigned int count, unsigned char force_update)
{
	unsigned int i;

	for (i = 0; i < count; i++) {
		unsigned int cmd;

		cmd = table[i].cmd;
		switch (cmd) {
		case REGFLAG_DELAY:
			MDELAY(table[i].count);
			break;

		case REGFLAG_END_OF_TABLE:
			break;

		default:
			dsi_set_cmdq_V2(cmd,
				table[i].count,
				table[i].para_list,
				force_update);
		}
	}
}


/* ----------------------------------------------------------------- */
/*  LCM Driver Implementations */
/* ----------------------------------------------------------------- */
static void lcm_set_util_funcs(const struct LCM_UTIL_FUNCS *util)
{
	memcpy(&lcm_util, util, sizeof(struct LCM_UTIL_FUNCS));
}

static void lcm_get_params(struct LCM_PARAMS *params)
{
	memset(params, 0, sizeof(struct LCM_PARAMS));

	params->type   = LCM_TYPE_DSI;

	params->width  = FRAME_WIDTH;
	params->height = FRAME_HEIGHT;

#if (LCM_DSI_CMD_MODE)
	params->dsi.mode                    = CMD_MODE;
#else
	params->dsi.mode                    = SYNC_EVENT_VDO_MODE;
#endif

	params->dsi.LANE_NUM                = LCM_FOUR_LANE;
	params->dsi.data_format.color_order = LCM_COLOR_ORDER_RGB;
	params->dsi.data_format.trans_seq   = LCM_DSI_TRANS_SEQ_MSB_FIRST;
	params->dsi.data_format.padding     = LCM_DSI_PADDING_ON_LSB;
	params->dsi.data_format.format      = LCM_DSI_FORMAT_RGB888;

	params->dsi.packet_size             = 256;
	params->dsi.PS                      = LCM_PACKED_PS_24BIT_RGB888;

	params->dsi.vertical_sync_active    = 1;
	params->dsi.vertical_backporch      = 32;
	params->dsi.vertical_frontporch     = 155;
	params->dsi.vertical_active_line    = FRAME_HEIGHT;

	params->dsi.horizontal_sync_active  = 1;
	params->dsi.horizontal_backporch    = 12;
	params->dsi.horizontal_frontporch   = 55;
	params->dsi.horizontal_active_pixel = FRAME_WIDTH;

	/* params->dsi.ssc_disable             = 1; */
#if (LCM_DSI_CMD_MODE)
	params->dsi.PLL_CLOCK               = 474;
#else
	params->dsi.PLL_CLOCK               = 474;
#endif
}

#define RT4801_REG_WRITE(add, data) rt4801_write_byte(add, data)
#define RT4801_REG_READ(add) rt4801_read_byte(add)

static void lcm_init_lcm(void)
{
	pr_notice("[KE/LCM] %s() enter\n", __func__);

	lcm_vgp_supply_enable();
	MDELAY(5);

	lcm_set_gpio_output(GPIO_LCD_PWR_ENP, GPIO_OUT_ONE);
	lcm_set_gpio_output(GPIO_LCD_PWR_ENN, GPIO_OUT_ONE);
	MDELAY(100);
	/* set AVDD = 5.8V */
	RT4801_REG_WRITE(0x00, 0x12);
	/* set AVEE = -5.8V */
	RT4801_REG_WRITE(0x01, 0x12);
	/* enable AVDD & AVEE discharge*/
	RT4801_REG_WRITE(0x03, 0x3);

	lcm_set_gpio_output(GPIO_CTP_RST, GPIO_OUT_ONE);
	MDELAY(10);

	lcm_set_gpio_output(GPIO_LCD_RST, GPIO_OUT_ONE);
	MDELAY(10);
	lcm_set_gpio_output(GPIO_LCD_RST, GPIO_OUT_ZERO);
	MDELAY(10);
	lcm_set_gpio_output(GPIO_LCD_RST, GPIO_OUT_ONE);
	MDELAY(150);

	push_table(lcm_init_setting,
		sizeof(lcm_init_setting) / sizeof(struct LCM_setting_table),
		1);
}

static void lcm_suspend(void)
{
	pr_notice("[KE/LCM] %s() enter\n", __func__);

	push_table(lcm_suspend_setting,
		sizeof(lcm_suspend_setting) / sizeof(struct LCM_setting_table),
		1);
	lcm_set_gpio_output(GPIO_LCD_RST, GPIO_OUT_ZERO);
	MDELAY(5);
	lcm_set_gpio_output(GPIO_LCD_RST, GPIO_OUT_ONE);
	MDELAY(2);
	lcm_set_gpio_output(GPIO_LCD_RST, GPIO_OUT_ZERO);
	MDELAY(50);

	lcm_set_gpio_output(GPIO_CTP_RST, GPIO_OUT_ZERO);
	MDELAY(10);

	lcm_set_gpio_output(GPIO_LCD_PWR_ENN, GPIO_OUT_ZERO);
	lcm_set_gpio_output(GPIO_LCD_PWR_ENP, GPIO_OUT_ZERO);

	lcm_vgp_supply_disable();
}

static void lcm_resume(void)
{
	pr_notice("[KE/LCM] %s() enter\n", __func__);

	lcm_init_lcm();
}

static unsigned int lcm_ata_check(unsigned char *buffer)
{
	return 1;
}

struct LCM_DRIVER ft8201_wuxga_dsi_vdo_boe_lcm_drv = {
	.name               = "ft8201_wuxga_dsi_vdo_boe",
	.set_util_funcs     = lcm_set_util_funcs,
	.get_params         = lcm_get_params,
	.init               = lcm_init_lcm,
	.resume             = lcm_resume,
	.suspend            = lcm_suspend,
	.ata_check = lcm_ata_check,
};

